<template>
  <div>
  <el-row>
    <el-col :span="7"><div class="grid-content bg-purple">&nbsp;</div></el-col>
    <el-col :span="8"><div class="grid-content bg-purple-light" >
    <br /><br /><br /><br />
      <el-form :model="user" status-icon :rules="rules" ref="user" label-width="100px" class="demo-ruleForm">
        <el-form-item label="密码" prop="userPwd">
            <el-input type="password" v-model="user.userPwd" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="ensurePwd">
            <el-input type="password" v-model="user.ensurePwd" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="userEmail"
            :rules="[
              { required: true, message: '请输入邮箱地址', trigger: 'blur' },
              { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
            ]"
        >
          <el-input style="width: 300px;" v-model="user.userEmail"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="userTel">
          <el-input style="width: 300px;" v-model="user.userTel"></el-input>
        </el-form-item>

        <el-form-item>
            <el-button @click="submitForm('user')">提交</el-button>
            <el-button @click="resetForm('user')">重置</el-button>
        </el-form-item>
      </el-form>
    </div></el-col>
    <el-col :span="8"><div class="grid-content bg-purple"></div></el-col>
  </el-row>
  </div>

</template>

<script>
  export default{
    name:"updataUser",
    data(){
      var validatePass = (rule, value, callback) => {
              if (value === '') {
                callback(new Error('请输入密码'));
              } else {
                if (this.user.ensurePwd !== '') {
                  this.$refs.user.validateField('ensurePwd');
                }
                callback();
              }
            };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.user.userPwd) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return{
        user:{
          userName:"",
          userPwd:"",
          ensurePwd:"",
          userEmail:"",
          userTel:"",
        },
        rules: {
              userPwd: [
                { validator: validatePass, trigger: 'blur' }
              ],
              ensurePwd: [
                { validator: validatePass2, trigger: 'blur' }
              ]

            }
      }
    },
    methods:{
      selectUser(){
          var self = this
          var http = this.$http.post("api/user/selectUser",);
          http.then(function(rs){
              if(rs.data.code == 200){
                self.user.userEmail = rs.data.data.userEmail;
                self.user.userTel = rs.data.data.userTel;
              }
          })
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
              if (valid) {
              var self = this
              var http = this.$http.post("api/user/updateUser",this.$qs.stringify(this.user));
              http.then(function(rs){
                  if(rs.data.code == 200){
                    alert(rs.data.msg)
                  }
              })
              } else {
                console.log('error submit!!');
                return false;
              }
            });
      },
      resetForm(user) {
          this.$refs[user].resetFields();
      }
    },
    mounted(){
      this.user.userName = sessionStorage.getItem("name");
      this.selectUser();
    }
  }
</script>

<style>
</style>
